﻿// ===============================================================================
// DotNet.Platform 开发框架 2016 版权所有
// ===============================================================================

using System.Collections.Generic;
using System.Linq;
using DotNet.Collections;
using DotNet.Extensions;
using DotNet.Utility;
using DotNet.JCore;
using DotNet.JCore.Entity;
using System;
using DotNet.Data.Utilities;
using DotNet.Data;

namespace DotNet.JCore.Service
{
    /// <summary>
    /// 服务
    /// </summary>
    public class InspectorRegistrationSumService
    {
        private JCoreRepository<InspectorRegistrationSum> repos = new JCoreRepository<InspectorRegistrationSum>();
        public List<InspectorRegistrationSum> GetList(DateTime? startDate, DateTime? endDate, string PointID, string CreateId)
        {
            var query = repos.SQL.OrderByDesc(p => p.checkinTime);
            SetQuery(query, startDate, endDate, PointID, CreateId);
            return repos.Fetch(query);
        }
        /// <summary>
        /// 设置查询条件
        /// </summary>
        /// <param name="query">查询对象</param>
        /// <param name="search">数据对象</param>
        public void SetQuery(SQLQuery<InspectorRegistrationSum> query, DateTime? startDate, DateTime? endDate, string PointId, string CreateId)
        {
            if (PointId.IsNotEmpty())
            {
                PointId = PointId.Trim();
                query.Where(p => p.pointId == PointId);
            }
            if (startDate.HasValue)
            {
                var startDateDt = startDate.ToDateTime();
                query.Where(p => p.checkinTime >= startDateDt);
            }

            if (endDate.HasValue)
            {
                var endDateDt = endDate.ToDateTime();
                query.Where(p => p.checkinTime <= endDateDt);
            }
            if (CreateId.IsNotEmpty() && CreateId != "0")
            {
                CreateId = CreateId.Trim();
                query.Where(p => p.InspectorsID == CreateId);
            }
        }
        /// <summary>
        /// 获取对象分页集合
        /// </summary>
        /// <param name="pageCondition">分页对象</param>
        /// <param name="name">名称关键字</param>
        /// <param name="isEnabled">启用</param>
        public PageList<InspectorRegistrationSum> GetPageList(PaginationCondition pageCondition,
           DateTime? startDate, DateTime? endDate, string PointID, string CreateId)
        {
            pageCondition.SetDefaultOrder(nameof(InspectorRegistrationSum.checkinTime));
            var query = repos.PageQuery(pageCondition);
            if (startDate.HasValue)
            {
                var startDateDt = startDate.ToDateTime();
                query.Where(p => p.checkinTime >= startDateDt);
            }

            if (endDate.HasValue)
            {
                var endDateDt = endDate.ToDateTime();
                query.Where(p => p.checkinTime <= endDateDt);
            }

            if (PointID.IsNotEmpty())
            {
                PointID = PointID.Trim();
                query.Where(p => p.pointId == PointID);
            }
            if (CreateId.IsNotEmpty() && CreateId != "0")
            {
                CreateId = CreateId.Trim();
                query.Where(p => p.InspectorsID == CreateId);
            }
            return repos.Page(query);
        }
        ///  执行插入报表字段的存储过程，
        ///  主要是把当日的数据重新计算一遍
        /// </summary>
        public List<InspectorRegistrationSum> Proc_InspectorRegistrationSum(string procname)
        {
            var db = repos.Database;
            var store = new StoreProc(procname);
            var list = db.Query<InspectorRegistrationSum>(store).ToList();
            return list;
        }
        /// <summary>
        /// 获取对象（手机版用）
        /// </summary>
        /// <param name="pointName"></param>
        /// <param name="checkinTime"></param>
        /// <returns></returns>
        //public InspectorRegistrationSum Get(string InspectorsName, DateTime checkinTime)
        //{
        //    return repos.Get(p => p.InspectorsName == InspectorsName&&p.checkinTime== checkinTime);
        //}
        /// <summary>
        /// 获取对象分页集合（手机版用）
        /// </summary>
        /// <param name="pageCondition"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="CreateId"></param>
        /// <returns></returns>
        public PageList<InspectorRegistrationSum> M_GetPageList(PaginationCondition pageCondition,string InspectorsName, DateTime? checkinTime)
        {
            pageCondition.SetDefaultOrder(nameof(InspectorRegistrationSum.checkinTime));
            var query = repos.PageQuery(pageCondition);
            if (checkinTime.HasValue)
            {
                var startDateDt = checkinTime.ToDateTime();
                query.Where(p => p.checkinTime >= checkinTime);
            }
            if (InspectorsName.IsNotEmpty())
            {
                query.Where(p => p.InspectorsName == InspectorsName);
            }
            return repos.Page(query);
        }
        /// <summary>
        /// 获取对象分页集合（手机版用）
        /// </summary>
        /// <param name="pageCondition"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="CreateId"></param>
        /// <returns></returns>
        public PageList<InspectorRegistrationSum> M_GetPageList(PaginationCondition pageCondition,
          DateTime? startDate, DateTime? endDate,string InspectorsName)
        {
            pageCondition.SetDefaultOrder(nameof(InspectorRegistrationSum.checkinTime));
            var query = repos.PageQuery(pageCondition);
            if (startDate.HasValue)
            {
                var startDateDt = startDate.ToDateTime();
                query.Where(p => p.checkinTime >= startDateDt);
            }

            if (endDate.HasValue)
            {
                var endDateDt = endDate.ToDateTime();
                query.Where(p => p.checkinTime <= endDateDt);
            }
            if (InspectorsName.IsNotEmpty())
            {
                query.Where(p => p.InspectorsName == InspectorsName);
            }
            return repos.Page(query);
        }
    }
}
